package com.rongzhe.house.presenter;

import com.rongzhe.house.ui.view.smoothlist.SmoothListView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * Created by huang on 2017/12/31.
 */

public class PageContoller<T> implements SmoothListView.ISmoothListViewListener {

    public interface PageControlInteface<T> {
        void addData(List<T> data);
        void setData(List<T> data);
        void loadData(boolean append);
        SmoothListView getListView();
        void setAsNoData(boolean noData);
    }

    private static final int DEFAULT_MAX_DATA_ONE_PAGE = 5;
    private int mCurrentPage = 1;
    private int mMaxDataOnePage = DEFAULT_MAX_DATA_ONE_PAGE;
    private String mLastTimeReferesh = null;
    private PageControlInteface<T> mPageControlInteface;

    public int getMaxDataOnePage() {
        return mMaxDataOnePage;
    }

    public int getCurrentPage() {
        return mCurrentPage;
    }

    public PageContoller(PageControlInteface<T> pageControlInteface) {
        mPageControlInteface = pageControlInteface;
        pageControlInteface.getListView().setSmoothListViewListener(this);
    }

    public PageContoller(PageControlInteface<T> pageControlInteface, int maxDataOnePage) {
        this(pageControlInteface);
        mMaxDataOnePage = maxDataOnePage;
    }

    public void onDataLoaded(List<T> data, boolean append) {
        if (append) {
            mPageControlInteface.addData(data);
            if(data == null || data.size() == 0) {
                mPageControlInteface.getListView().setAsNoMore();
            }
        } else {
            mPageControlInteface.setData(data);
        }
        if (data == null || data.size() < 1) {
            mPageControlInteface.getListView().setNoFooterVisiblity(false);
            mPageControlInteface.getListView().setAsNoMore();
            if (!append) {
                mPageControlInteface.setAsNoData(true);
            } else {
                mPageControlInteface.setAsNoData(false);
            }
        } else if (mCurrentPage == 1 && data.size() < mMaxDataOnePage) {
            mPageControlInteface.getListView().setNoFooterVisiblity(false);
            mPageControlInteface.setAsNoData(false);
        } else if (data.size() < mMaxDataOnePage) {
            mPageControlInteface.getListView().setAsNoMore();
            mPageControlInteface.setAsNoData(false);
        } else {
            mPageControlInteface.setAsNoData(false);
        }
        if (mCurrentPage == 1) {
            mLastTimeReferesh = new SimpleDateFormat("HH:mm:ss")
                    .format(Calendar.getInstance().getTime());
        }
    }

    public void onDataLoadFinish(boolean append) {
        if (append) {
            mPageControlInteface.getListView().stopLoadMore();
        }
        mPageControlInteface.getListView().stopRefresh();
    }

    public void resetPage() {
        mCurrentPage = 1;
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        mPageControlInteface.getListView().setRefreshTime(mLastTimeReferesh );
        mPageControlInteface.loadData(false);
    }

    @Override
    public void onLoadMore() {
        mCurrentPage++;
        mPageControlInteface.loadData(true);
    }


}
